import React, {useEffect, useMemo, useState} from 'react';
import {Button, Flex, Form, Input, InputNumber, message, Modal, Space} from "antd";
import UploadBanner from "@/cms/UploadBanner";

function UseAddEdit({queryList}) {
    const [open,setOpen] = useState(false),
        [loading,setLoading] = useState(false),
        [form] = Form.useForm();
    function onCancel(){
        setOpen(false)
    }
    useEffect(()=>{
        if (open === false){
            form.resetFields();
        }else if(open.ID){
            form.setFieldsValue({
                ...open,
            })
        }else{
            form.setFieldsValue({
                phone_list:[{}]
            })
        }
    },[open])
    function onOk(){
        form.validateFields().then(values => {
            if (open.ID){
                values.ID=open.ID
            }
            ax.post("/api/addUser",{
                ...values,
                avatar:values.avatar.map(item=>({url:item.url||item.response.message}))
            }).then(res=>{
                message.success(res.msg);
                queryList();
                onCancel();
            })
        })
    }
    function changeStatus(status){
        ax.post("/app/teaerRegister",{
            ...open,
            status
        }).then(res=>{
            message.success(res.msg);
            queryList();
            onCancel();
        })
    }
    function onAgree(){
        changeStatus(1);
        ax.post("/app/addTeaerAccount",{
            phone:open.phone,
            password:"e10adc3949ba59abbe56e057f20f883e",
            role:"茶艺师",
            userId:open.ID,
            realName:open.name,
            openid:open.openid
        })
    }
    return {
        setOpen,
        ModalDOM:<Modal
            title={open?.ID?"查看":"添加"}
            open={open}
            onOk={onOk}
            onCancel={onCancel}
            confirmLoading={loading}
            width={800}
            footer={null}
        >
            <Form
                labelCol={{span: 5}}
                wrapperCol={{span: 14}}
                form={form}
            >
                <Form.Item
                    label={"姓名"}
                    name={"name"}
                    rules={[
                        {
                            required: true,
                            message: '请输入姓名!',
                        },
                    ]}
                >
                    <Input placeholder={"请输入"} />
                </Form.Item>
                <Form.Item
                    label={"性别"}
                    name={"gender"}
                    rules={[
                        {
                            required: true,
                            message: '请输入姓名!',
                        },
                    ]}
                >
                    <Input placeholder={"请输入"}/>
                </Form.Item>
                <Form.Item
                    label={"手机号"}
                    name={"phone"}
                    rules={[
                        {
                            required: true,
                            message: '请输入手机号!',
                        },
                    ]}
                >
                    <Input placeholder={"请输入"}/>
                </Form.Item>
                <Form.Item
                    label={"地址"}
                    name={"address"}
                >
                    <Input.TextArea placeholder={"请输入"} autoSize />
                </Form.Item>
                <Form.Item
                    label={"单价"}
                    name={"price"}
                >
                    <InputNumber style={{width:"100%"}} placeholder={"一小时费用"}/>
                </Form.Item>
                <Form.Item
                    label={"员工肖像"}
                    name={"avatar"}
                >
                    <UploadBanner />
                </Form.Item>
                <Form.Item
                    label={"证件照"}
                    name={"idCard"}
                >
                    <UploadBanner />
                </Form.Item>
                <Form.Item
                    label={"职业资格证书"}
                    name={"certificate"}
                >
                    <UploadBanner />
                </Form.Item>
                <Form.Item
                    label={"介绍"}
                    name={"desc"}
                >
                    <Input.TextArea placeholder={"茶艺师个人介绍"} autoSize/>
                </Form.Item>
            </Form>
            <Flex justify={"end"}>
                <Space>
                    {
                        open.status === 0 && <Button type={"primary"} onClick={()=>onAgree()}>通过</Button>
                    }
                    {
                        open.status === 1 && <Button type={"primary"} onClick={()=>changeStatus(2)}>上班</Button>
                    }
                    {
                        open.status === 2 && <Button type={"primary"} onClick={()=>changeStatus(1)}>下班</Button>
                    }

                </Space>
            </Flex>
        </Modal>
    }
}

export default UseAddEdit;